home *** CD-ROM | disk | FTP | other *** search
- package javax.swing.tree;
-
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- import java.util.Enumeration;
- import java.util.NoSuchElementException;
- import java.util.Vector;
-
- public class DefaultMutableTreeNode implements Cloneable, MutableTreeNode, Serializable {
- public static final Enumeration EMPTY_ENUMERATION = new 1();
- protected MutableTreeNode parent;
- protected Vector children;
- protected transient Object userObject;
- protected boolean allowsChildren;
-
- public DefaultMutableTreeNode() {
- this((Object)null);
- }
-
- public DefaultMutableTreeNode(Object var1) {
- this(var1, true);
- }
-
- public DefaultMutableTreeNode(Object var1, boolean var2) {
- this.parent = null;
- this.allowsChildren = var2;
- this.userObject = var1;
- }
-
- public void add(MutableTreeNode var1) {
- if (var1 != null && var1.getParent() == this) {
- this.insert(var1, this.getChildCount() - 1);
- } else {
- this.insert(var1, this.getChildCount());
- }
-
- }
-
- public Enumeration breadthFirstEnumeration() {
- return new BreadthFirstEnumeration(this, this);
- }
-
- public Enumeration children() {
- return this.children == null ? EMPTY_ENUMERATION : this.children.elements();
- }
-
- public Object clone() {
- DefaultMutableTreeNode var1 = null;
-
- try {
- var1 = (DefaultMutableTreeNode)super.clone();
- var1.children = null;
- var1.parent = null;
- return var1;
- } catch (CloneNotSupportedException var3) {
- throw new InternalError(((Throwable)var3).toString());
- }
- }
-
- public Enumeration depthFirstEnumeration() {
- return this.postorderEnumeration();
- }
-
- public boolean getAllowsChildren() {
- return this.allowsChildren;
- }
-
- public TreeNode getChildAfter(TreeNode var1) {
- if (var1 == null) {
- throw new IllegalArgumentException("argument is null");
- } else {
- int var2 = this.getIndex(var1);
- if (var2 == -1) {
- throw new IllegalArgumentException("node is not a child");
- } else {
- return var2 < this.getChildCount() - 1 ? this.getChildAt(var2 + 1) : null;
- }
- }
- }
-
- public TreeNode getChildAt(int var1) {
- if (this.children == null) {
- throw new ArrayIndexOutOfBoundsException("node has no children");
- } else {
- return (TreeNode)this.children.elementAt(var1);
- }
- }
-
- public TreeNode getChildBefore(TreeNode var1) {
- if (var1 == null) {
- throw new IllegalArgumentException("argument is null");
- } else {
- int var2 = this.getIndex(var1);
- if (var2 == -1) {
- throw new IllegalArgumentException("argument is not a child");
- } else {
- return var2 > 0 ? this.getChildAt(var2 - 1) : null;
- }
- }
- }
-
- public int getChildCount() {
- return this.children == null ? 0 : this.children.size();
- }
-
- public int getDepth() {
- Object var1 = null;
-
- for(Enumeration var2 = this.breadthFirstEnumeration(); var2.hasMoreElements(); var1 = var2.nextElement()) {
- }
-
- if (var1 == null) {
- throw new InternalError("nodes should be null");
- } else {
- return ((DefaultMutableTreeNode)var1).getLevel() - this.getLevel();
- }
- }
-
- public TreeNode getFirstChild() {
- if (this.getChildCount() == 0) {
- throw new NoSuchElementException("node has no children");
- } else {
- return this.getChildAt(0);
- }
- }
-
- public DefaultMutableTreeNode getFirstLeaf() {
- DefaultMutableTreeNode var1;
- for(var1 = this; !var1.isLeaf(); var1 = (DefaultMutableTreeNode)var1.getFirstChild()) {
- }
-
- return var1;
- }
-
- public int getIndex(TreeNode var1) {
- if (var1 == null) {
- throw new IllegalArgumentException("argument is null");
- } else {
- return !this.isNodeChild(var1) ? -1 : this.children.indexOf(var1);
- }
- }
-
- public TreeNode getLastChild() {
- if (this.getChildCount() == 0) {
- throw new NoSuchElementException("node has no children");
- } else {
- return this.getChildAt(this.getChildCount() - 1);
- }
- }
-
- public DefaultMutableTreeNode getLastLeaf() {
- DefaultMutableTreeNode var1;
- for(var1 = this; !var1.isLeaf(); var1 = (DefaultMutableTreeNode)var1.getLastChild()) {
- }
-
- return var1;
- }
-
- public int getLeafCount() {
- int var1 = 0;
- Enumeration var3 = this.breadthFirstEnumeration();
-
- while(var3.hasMoreElements()) {
- TreeNode var2 = (TreeNode)var3.nextElement();
- if (var2.isLeaf()) {
- ++var1;
- }
- }
-
- if (var1 < 1) {
- throw new InternalError("tree has zero leaves");
- } else {
- return var1;
- }
- }
-
- public int getLevel() {
- int var2 = 0;
-
- for(Object var1 = this; (var1 = ((TreeNode)var1).getParent()) != null; ++var2) {
- }
-
- return var2;
- }
-
- public DefaultMutableTreeNode getNextLeaf() {
- DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
- if (var2 == null) {
- return null;
- } else {
- DefaultMutableTreeNode var1 = this.getNextSibling();
- return var1 != null ? var1.getFirstLeaf() : var2.getNextLeaf();
- }
- }
-
- public DefaultMutableTreeNode getNextNode() {
- if (this.getChildCount() != 0) {
- return (DefaultMutableTreeNode)this.getChildAt(0);
- } else {
- DefaultMutableTreeNode var1 = this.getNextSibling();
- if (var1 != null) {
- return var1;
- } else {
- for(DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent(); var2 != null; var2 = (DefaultMutableTreeNode)var2.getParent()) {
- var1 = var2.getNextSibling();
- if (var1 != null) {
- return var1;
- }
- }
-
- return null;
- }
- }
- }
-
- public DefaultMutableTreeNode getNextSibling() {
- DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
- DefaultMutableTreeNode var1;
- if (var2 == null) {
- var1 = null;
- } else {
- var1 = (DefaultMutableTreeNode)var2.getChildAfter(this);
- }
-
- if (var1 != null && !this.isNodeSibling(var1)) {
- throw new InternalError("child of parent is not a sibling");
- } else {
- return var1;
- }
- }
-
- public TreeNode getParent() {
- return this.parent;
- }
-
- public TreeNode[] getPath() {
- return this.getPathToRoot(this, 0);
- }
-
- protected TreeNode[] getPathToRoot(TreeNode var1, int var2) {
- TreeNode[] var3;
- if (var1 == null) {
- if (var2 == 0) {
- return null;
- }
-
- var3 = new TreeNode[var2];
- } else {
- ++var2;
- var3 = this.getPathToRoot(var1.getParent(), var2);
- var3[var3.length - var2] = var1;
- }
-
- return var3;
- }
-
- public DefaultMutableTreeNode getPreviousLeaf() {
- DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
- if (var2 == null) {
- return null;
- } else {
- DefaultMutableTreeNode var1 = this.getPreviousSibling();
- return var1 != null ? var1.getLastLeaf() : var2.getPreviousLeaf();
- }
- }
-
- public DefaultMutableTreeNode getPreviousNode() {
- DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
- if (var2 == null) {
- return null;
- } else {
- DefaultMutableTreeNode var1 = this.getPreviousSibling();
- if (var1 != null) {
- return var1.getChildCount() == 0 ? var1 : var1.getLastLeaf();
- } else {
- return var2;
- }
- }
- }
-
- public DefaultMutableTreeNode getPreviousSibling() {
- DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
- DefaultMutableTreeNode var1;
- if (var2 == null) {
- var1 = null;
- } else {
- var1 = (DefaultMutableTreeNode)var2.getChildBefore(this);
- }
-
- if (var1 != null && !this.isNodeSibling(var1)) {
- throw new InternalError("child of parent is not a sibling");
- } else {
- return var1;
- }
- }
-
- public TreeNode getRoot() {
- Object var1 = this;
-
- Object var2;
- do {
- var2 = var1;
- var1 = ((TreeNode)var1).getParent();
- } while(var1 != null);
-
- return (TreeNode)var2;
- }
-
- public TreeNode getSharedAncestor(DefaultMutableTreeNode var1) {
- if (var1 == this) {
- return this;
- } else if (var1 == null) {
- return null;
- } else {
- int var2 = this.getLevel();
- int var3 = var1.getLevel();
- int var4;
- Object var5;
- Object var6;
- if (var3 > var2) {
- var4 = var3 - var2;
- var5 = var1;
- var6 = this;
- } else {
- var4 = var2 - var3;
- var5 = this;
- var6 = var1;
- }
-
- while(var4 > 0) {
- var5 = ((TreeNode)var5).getParent();
- --var4;
- }
-
- while(var5 != var6) {
- var5 = ((TreeNode)var5).getParent();
- var6 = ((TreeNode)var6).getParent();
- if (var5 == null) {
- if (var5 == null && var6 == null) {
- return null;
- }
-
- throw new InternalError("nodes should be null");
- }
- }
-
- return (TreeNode)var5;
- }
- }
-
- public int getSiblingCount() {
- TreeNode var1 = this.getParent();
- return var1 == null ? 1 : var1.getChildCount();
- }
-
- public Object getUserObject() {
- return this.userObject;
- }
-
- public Object[] getUserObjectPath() {
- TreeNode[] var1 = this.getPath();
- Object[] var2 = new Object[var1.length];
-
- for(int var3 = 0; var3 < var1.length; ++var3) {
- var2[var3] = ((DefaultMutableTreeNode)var1[var3]).getUserObject();
- }
-
- return var2;
- }
-
- public void insert(MutableTreeNode var1, int var2) {
- if (!this.allowsChildren) {
- throw new IllegalStateException("node does not allow children");
- } else if (var1 == null) {
- throw new IllegalArgumentException("new child is null");
- } else if (this.isNodeAncestor(var1)) {
- throw new IllegalArgumentException("new child is an ancestor");
- } else {
- MutableTreeNode var3 = (MutableTreeNode)var1.getParent();
- if (var3 != null) {
- var3.remove(var1);
- }
-
- var1.setParent(this);
- if (this.children == null) {
- this.children = new Vector();
- }
-
- this.children.insertElementAt(var1, var2);
- }
- }
-
- public boolean isLeaf() {
- return this.getChildCount() == 0;
- }
-
- public boolean isNodeAncestor(TreeNode var1) {
- if (var1 == null) {
- return false;
- } else {
- Object var2 = this;
-
- while(var2 != var1) {
- if ((var2 = ((TreeNode)var2).getParent()) == null) {
- return false;
- }
- }
-
- return true;
- }
- }
-
- public boolean isNodeChild(TreeNode var1) {
- boolean var2;
- if (var1 == null) {
- var2 = false;
- } else if (this.getChildCount() == 0) {
- var2 = false;
- } else {
- var2 = var1.getParent() == this;
- }
-
- return var2;
- }
-
- public boolean isNodeDescendant(DefaultMutableTreeNode var1) {
- return var1 == null ? false : var1.isNodeAncestor(this);
- }
-
- public boolean isNodeRelated(DefaultMutableTreeNode var1) {
- return var1 != null && this.getRoot() == var1.getRoot();
- }
-
- public boolean isNodeSibling(TreeNode var1) {
- boolean var2;
- if (var1 == null) {
- var2 = false;
- } else if (var1 == this) {
- var2 = true;
- } else {
- TreeNode var3 = this.getParent();
- var2 = var3 != null && var3 == var1.getParent();
- if (var2 && !((DefaultMutableTreeNode)this.getParent()).isNodeChild(var1)) {
- throw new InternalError("sibling has different parent");
- }
- }
-
- return var2;
- }
-
- public boolean isRoot() {
- return this.getParent() == null;
- }
-
- public Enumeration pathFromAncestorEnumeration(TreeNode var1) {
- return new PathBetweenNodesEnumeration(this, var1, this);
- }
-
- public Enumeration postorderEnumeration() {
- return new PostorderEnumeration(this, this);
- }
-
- public Enumeration preorderEnumeration() {
- return new PreorderEnumeration(this, this);
- }
-
- private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
- var1.defaultReadObject();
- Object[] var2 = var1.readObject();
- if (var2.length > 0 && var2[0].equals("userObject")) {
- this.userObject = var2[1];
- }
-
- }
-
- public void remove(int var1) {
- MutableTreeNode var2 = (MutableTreeNode)this.getChildAt(var1);
- this.children.removeElementAt(var1);
- var2.setParent((MutableTreeNode)null);
- }
-
- public void remove(MutableTreeNode var1) {
- if (var1 == null) {
- throw new IllegalArgumentException("argument is null");
- } else if (!this.isNodeChild(var1)) {
- throw new IllegalArgumentException("argument is not a child");
- } else {
- this.remove(this.getIndex(var1));
- }
- }
-
- public void removeAllChildren() {
- for(int var1 = this.getChildCount() - 1; var1 >= 0; --var1) {
- this.remove(var1);
- }
-
- }
-
- public void removeFromParent() {
- MutableTreeNode var1 = (MutableTreeNode)this.getParent();
- if (var1 != null) {
- var1.remove(this);
- }
-
- }
-
- public void setAllowsChildren(boolean var1) {
- if (var1 != this.allowsChildren) {
- this.allowsChildren = var1;
- if (!this.allowsChildren) {
- this.removeAllChildren();
- }
- }
-
- }
-
- public void setParent(MutableTreeNode var1) {
- this.parent = var1;
- }
-
- public void setUserObject(Object var1) {
- this.userObject = var1;
- }
-
- public String toString() {
- return this.userObject == null ? null : this.userObject.toString();
- }
-
- private void writeObject(ObjectOutputStream var1) throws IOException {
- var1.defaultWriteObject();
- Object[] var2;
- if (this.userObject != null && this.userObject instanceof Serializable) {
- var2 = new Object[]{"userObject", this.userObject};
- } else {
- var2 = new Object[0];
- }
-
- var1.writeObject(var2);
- }
- }
-